package com.blog.admin.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.blog.admin.service.TagService;
import com.blog.common.core.web.domain.AjaxResult;
import com.blog.common.domain.dto.ConditionDto;
import com.blog.common.domain.dto.TagDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;

/**
 * <p>
 * 标签后台控制器
 * </p>
 *
 * @author Valerie
 * @date 2023/3/12
 **/
@RestController
@Api(tags = "标签后台控制器")
public class TagBackController {

    @Resource
    private TagService tagService;

    /**
     * 根据条件搜索标签
     * @param condition 条件
     * @return AjaxResult
     */
    @GetMapping("/tags/search")
    @ApiOperation(value = "根据条件搜索标签")
    public AjaxResult listTagsBySearch(ConditionDto condition){
        return AjaxResult.success(tagService.listTagsBySearch(condition));
    }

    /**
     * 获取标签列表
     * @param condition 条件
     * @return AjaxResult
     */
    @GetMapping("/tags/list")
    @ApiOperation(value = "获取标签列表")
    public AjaxResult listTags(ConditionDto condition){
        return AjaxResult.success(tagService.listTags(condition));
    }

    /**
     * 根据id获取标签
     * @param tagId id
     * @return AjaxResult
     */
    @GetMapping("/tags")
    @ApiOperation(value = "获取标签列表")
    public AjaxResult getTag(@RequestParam("tagId") Serializable tagId){
        return AjaxResult.success(tagService.getTagById(tagId));
    }

    /**
     * 新增标签
     *
     * @param tags 标签名
     * @return {@link AjaxResult}
     */
    @PostMapping("/tags")
    @ApiOperation("新增标签")
    public AjaxResult saveTag(@RequestBody JsonNode tags){
        tagService.saveTag(tags.get("tagName").asText());
        return AjaxResult.success();
    }

    /**
     * 修改标签
     *
     * @param tagDto 标签
     * @return {@link AjaxResult}
     */
    @PutMapping("/tags")
    @ApiOperation("修改标签")
    public AjaxResult updateTag(@RequestBody TagDto tagDto){
        tagService.updateTag(tagDto);
        return AjaxResult.success();
    }

}
